package de.bazinga.rotw.network.message;

import com.captiveimagination.jgn.message.Message;
import com.captiveimagination.jgn.message.type.CertifiedMessage;
import com.captiveimagination.jgn.message.type.PlayerMessage;
import com.captiveimagination.jgn.message.type.TimestampedMessage;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;

import de.bazinga.rotw.game.RotwGameState;
import de.bazinga.rotw.network.Server;
import de.bazinga.rotw.person.OpponentList;
import de.bazinga.rotw.person.Person;
import de.bazinga.rotw.weapon.Knife;
import de.bazinga.rotw.weapon.Pistol;
import de.bazinga.rotw.weapon.Rifle;
import de.bazinga.rotw.weapon.Uzi;
import de.bazinga.rotw.weapon.Weapon;
import de.bazinga.rotw.weapon.grenade.GrenadeList;
import de.bazinga.rotw.weapon.grenade.GrenadeObject;
import de.bazinga.rotw.weapon.grenade.HEGrenade;
import de.bazinga.rotw.weapon.grenade.SmokeGrenade;

public class NewBulletMessage extends Message implements CertifiedMessage, PlayerMessage, TimestampedMessage, GameMessage {
	public static final int PISTOLGUN = 1;
	public static final int RIFLEGUN = 2;
	public static final int KNIFE = 3;
	public static final int UZI = 4;
	public static final int VEHICLE = 5;
	public static final int SMOKEGRANADE = 6;
	public static final int HEGRANADE = 7;
	public static final int BASEATTACKER = 8;
	
	private Vector3f location, direction;
	private ColorRGBA bulletColor = ColorRGBA.red;
	
	private int weaponFlag = -1;

	public Vector3f getLocation() {
		return location;
	}

	public void setLocation(Vector3f location) {
		this.location = location;
	}

	public Vector3f getDirection() {
		return direction;
	}

	public void setDirection(Vector3f direction) {
		this.direction = direction;
	}

	public void setWeaponFlag(int weaponFlag) {
		this.weaponFlag = weaponFlag;
	}

	public int getWeaponFlag() {
		return weaponFlag;
	}

	public ColorRGBA getBulletColor() {
		return bulletColor;
	}

	public void setBulletColor(ColorRGBA bulletColor) {
		this.bulletColor = bulletColor;
	}
	
	
	public void handleMessage(RotwGameState game, Message msg, boolean server) {
		if(!(msg instanceof NewBulletMessage)) return ;

		NewBulletMessage message = (NewBulletMessage)msg;
		
		if(server) {
			if(OpponentList.getInstance().getOpponent(message.getPlayerId()) != null) {
    			
				Class<? extends Weapon> weaponClass = null;
    			
    			Person opp = OpponentList.getInstance().getOpponent(message.getPlayerId());
    			if(message.getWeaponFlag() == NewBulletMessage.PISTOLGUN && opp.getWeapon(Pistol.class) != null) {
        			//Broadcast to all clients so they can play a sound
    				((Server)game.getSyncManager()).getServer().sendToAllExcept(message, message.getPlayerId());

    				Pistol.otherPlayerFired(message.getLocation());
    				
    				weaponClass = Pistol.class;
    			}else if(message.getWeaponFlag() == NewBulletMessage.RIFLEGUN && opp.getWeapon(Rifle.class) != null) {
        			//Broadcast to all clients so they can play a sound
    				((Server)game.getSyncManager()).getServer().sendToAllExcept(message, message.getPlayerId());

    				Rifle.otherPlayerFired(message.getLocation());
    				weaponClass = Rifle.class;
    			}else if(message.getWeaponFlag() == NewBulletMessage.KNIFE && opp.getWeapon(Knife.class) != null) {
        			//Broadcast to all clients so they can play a sound
    				((Server)game.getSyncManager()).getServer().sendToAllExcept(message, message.getPlayerId());

    				Knife.otherPlayerFired(message.getLocation());
    				weaponClass = Knife.class;
    			}else if(message.getWeaponFlag() == NewBulletMessage.UZI && opp.getWeapon(Uzi.class) != null) {
        			//Broadcast to all clients so they can play a sound
    				((Server)game.getSyncManager()).getServer().sendToAllExcept(message, message.getPlayerId());

    				Uzi.otherPlayerFired(message.getLocation());
    				weaponClass = Uzi.class;
    			}else if(message.getWeaponFlag() == NewBulletMessage.SMOKEGRANADE && opp.getWeapon(SmokeGrenade.class) != null) {
    				// Broadcast this NewBullet to all Clients
        			((Server)game.getSyncManager()).getServer().sendToAll(message);
        			
        			weaponClass = SmokeGrenade.class;
    			}else if(message.getWeaponFlag() == NewBulletMessage.HEGRANADE && opp.getWeapon(HEGrenade.class) != null) {
    				// Broadcast this NewBullet to all Clients
        			((Server)game.getSyncManager()).getServer().sendToAll(message);
        			
        			weaponClass = HEGrenade.class;
    			}
    			
    			if(weaponClass != null) {
	    			opp.getWeapon(weaponClass).fireBullet(
							opp, 
							message.getLocation(),
							message.getDirection(),
							true);
    			}
    		}
		}else{
			
			// Somebody shot a bullet, just display and move it
			if(message.getWeaponFlag() == NewBulletMessage.PISTOLGUN) {
				Pistol.otherPlayerFired(message.getLocation());
			}else if(message.getWeaponFlag() == NewBulletMessage.KNIFE) {
				Knife.otherPlayerFired(message.getLocation());
			}else if(message.getWeaponFlag() == NewBulletMessage.RIFLEGUN) {
				Rifle.otherPlayerFired(message.getLocation());
			}else if(message.getWeaponFlag() == NewBulletMessage.UZI) {
				Uzi.otherPlayerFired(message.getLocation());
			}else if(message.getWeaponFlag() == NewBulletMessage.SMOKEGRANADE) {        		
        		GrenadeObject granade = null;
    			granade = GrenadeList.getInstance().getUnusedGranade(
    						new SmokeGrenade(),
    						null,
    						game.getRootNode(), 
    						location, 
    						direction);
    			GrenadeList.getInstance().addGranade(granade);
      
        	}else if(message.getWeaponFlag() == NewBulletMessage.HEGRANADE) {        		
        		GrenadeObject granade = null;
    			granade = GrenadeList.getInstance().getUnusedGranade(
    						new HEGrenade(),
    						null,
    						game.getRootNode(), 
    						location, 
    						direction);
    			GrenadeList.getInstance().addGranade(granade);
      
        	}
		}
	}
	
}
